#!/usr/bin/perl

######################################################################
package Swignition::FeedParser;
######################################################################

use HTML::Entities qw(decode_entities encode_entities_numeric);
use Swignition::RdfXmlParser;
use Swignition::Export;
use Swignition::Export::BlogEntry;
use Swignition::Export::Entity;
use utf8;
use strict;

use vars qw(@ISA);
@ISA = qw(Swignition::RdfXmlParser);

sub new
{
	my $class    = shift;
	my $source   = shift;
	my $opts     = shift;
	my $this     = Swignition::GenericParser::object_structure($source, $opts);
	
	bless $this, $class;	
	
	$this->find_base_uri;
	
	# Knowing about these prefixes makes RDF output a little prettier.
	$this->mergeNS('rdf',      'http://www.w3.org/1999/02/22-rdf-syntax-ns#', 'RDF', -1, 0);
	$this->mergeNS('rss',      'http://purl.org/rss/1.0/',                'RSS 1.0', -1, 0);
	$this->mergeNS('rss091',   'http://purl.org/rss/1.0/modules/rss091#', 'RSS 0.91', -1, 0);
	$this->mergeNS('dc',       'http://purl.org/dc/elements/1.1/',        'Dublin Core 1.1', -1, 0);
	$this->mergeNS('dcterms',  'http://purl.org/dc/terms/',               'Dublin Core Terms', -1, 0);
	$this->mergeNS('content',  'http://purl.org/rss/1.0/modules/content/','RSS Content Module', -1, 0);
	$this->mergeNS('contentx', 'http://web.resource.org/rss/1.0/modules/content/', 'RSS Content Module (Erroneous URI used by some versions of Redland)', -1, 0);
	$this->mergeNS('non-rss',  'http://www.w3.org/2005/',                 'Non-RSS Atom Stuff (silly Redland)', -1, 0);

	if ($this->{options}->{p_AUTO})
	{
		$this->redland_parse('rss-tag-soup');

		Swignition::GenericParser::HTTP::consume($this)
			if Swignition::GenericParser::HTTP::is_enabled($this);

		if ($this->{options}->{p_uf})
		{
			$this->parse_uf;
		}

		Swignition::GenericParser::Recursive::consume($this)
			if Swignition::GenericParser::Recursive::is_enabled($this);
		
		$this->smush;
	}

	return $this;	
}

sub parse_uf
{
	my $this = shift;
	
	foreach my $subject (keys %{$this->{RDF}})
	{
		if (Swignition::Export::BlogEntry::is($this->{RDF}->{$subject}))
		{
			my $contentEncoded = firstOf($this->{RDF}->{$subject},
				'http://purl.org/rss/1.0/modules/content/encoded',
				'http://web.resource.org/rss/1.0/modules/content/encoded');
			my $html = sprintf("<html>%s</html>", decode_entities($contentEncoded));
			my $HTML = Swignition::HtmlParser->new($html, {
				'p_AUTO'       => 1,
				'p_comments'   => 0,
				'p_erdf'       => 0,
				'p_grddl'      => 0,
				'p_http'       => 0,
				'p_metatags'   => 0,
				'p_rdf'        => 0,
				'p_rdfa'       => $this->{options}->{p_rdfa},
				'p_rdfx'       => 0,
				'p_structure'  => 0,
				'p_uf'         => $this->{options}->{p_uf},
				'rdfa_strict_doctype'   => 0,
				'rdfa_strict_version'   => 0,
				'rdfa_strings' => $this->{options}->{rdfa_strings},
				'smush'        => 0,
				'uf_strict_profiles'    => 0,
				'base' => $subject
			});
				
			# If we've actually managed to parse something...
			if ($HTML)
			{
				# Export what we got as Turtle
				my $turtle = Swignition::Export::Entity::multiTurtle( $HTML->{RDF} );
				# Then merge the Turtle into our graph.
				Swignition::GenericParser::RDF::handle($this, $turtle, $subject, 'turtle');
			}
			
		}
	}

#	exit;

}

1;
